package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.server.event.model.OrderInputHistoryEvent;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.PlaceInputHistory;
import com.ybkj.daijia.server.service.PlaceInputHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderInputHistoryListener implements ApplicationListener<OrderInputHistoryEvent> {

    private static final Logger logger = LoggerFactory.getLogger(OrderInputHistoryListener.class);

    @Autowired
    private PlaceInputHistoryService placeInputHistoryService;

    @Override
    @Async
    public void onApplicationEvent(OrderInputHistoryEvent event) {

        Order order = (Order) event.getSource();

        logger.debug("order inputhistory event:{}", order.getOrderNumber());

        //记录历史输入地址
        PlaceInputHistory record = placeInputHistoryService.findByAddress(order.getFromPlace());

        if (null == record) {
            record = new PlaceInputHistory();
            record.setAddress(order.getFromPlace());
            record.setLatitude(order.getClientLatitude());
            record.setLongitude(order.getClientLongitude());
            placeInputHistoryService.insertRecord(record);
        }

    }

}
